using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public delegate void CallBack();
public delegate void CallBack<T>(T t);
public delegate void CallBack<T,U>(T t,U u);
public class MessMgr : Singleton<MessMgr>
{
    private Dictionary<string, Delegate> dic = new Dictionary<string, Delegate>();

    public void AddListener(string name, CallBack callBack)
    {
        OnListenr(name);
        dic[name] = (CallBack)dic[name] + callBack;
    }
    public void AddListener<T>(string name, CallBack<T> callBack)
    {
        OnListenr(name);
        dic[name] = (CallBack<T>)dic[name] + callBack;
    }
    public void AddListener<T,U>(string name, CallBack<T,U> callBack)
    {
        OnListenr(name);
        dic[name] = (CallBack<T,U>)dic[name] + callBack;
    }
    public void RemoveListener(string name,CallBack callBack)
    {
        if (dic.ContainsKey(name))
        {
            dic[name] = (CallBack)dic[name] - callBack;
            if (dic[name] == null)
            {
                dic.Remove(name);
            }
        }
    }
    public void RemoveListener<T>(string name, CallBack<T> callBack)
    {
        if (dic.ContainsKey(name))
        {
            dic[name] = (CallBack<T>)dic[name] - callBack;
            if (dic[name] == null)
            {
                dic.Remove(name);
            }
        }
    }
    public void RemoveListener<T,U>(string name, CallBack<T,U> callBack)
    {
        if (dic.ContainsKey(name))
        {
            dic[name] = (CallBack<T,U>)dic[name] - callBack;
            if (dic[name] == null)
            {
                dic.Remove(name);
            }
        }
    }
    public void BroadCast(string name)
    {
        if (dic.ContainsKey(name))
        {
            CallBack callBack = dic[name] as CallBack;
            callBack?.Invoke();
        }
    }
    public void BroadCast<T>(string name, T t)
    {
        if (dic.ContainsKey(name))
        {
            CallBack<T> callBack = dic[name] as CallBack<T>;
            callBack?.Invoke(t);
        }
    }
    public void BroadCast<T,U>(string name, T t, U u)
    {
        if (dic.ContainsKey(name))
        {
            CallBack<T,U> callBack = dic[name] as CallBack<T,U>;
            callBack?.Invoke(t, u);
        }
    }
    private void OnListenr(string name)
    {
        if (!dic.ContainsKey(name))
        {
            dic[name] = null;
        }
    }
}
